
//
//  YZMOriginalView.swift
//  YZMweibo
//
//  Created by Simon on 16/4/21.
//  Copyright © 2016年 Simon. All rights reserved.
//

import UIKit
import SnapKit
class YZMOriginalView: UIView {
    // 记录我们原创微博的底部约束
    var originalViewBottomConstraint: Constraint?
    
    var status: YZMStatus?{
        didSet{
            
            // 头像
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            // 昵称
            nameLabel.text = status?.user?.screen_name
            // 会员等级
            mbrankImageView.image = getmbrankImage(status?.user?.mbrank ?? 0)
            // 时间(后期处理)
            timeLable.text = "时间"
            // 来自(后期处理)
            sourceLabel.text = "来自传智播客27期微博"
            // 认证
            verifiedImageView.image = getverifiedImage(status?.user?.verified_type ?? -1)
            // 微博正文
            contentLabel.text = status?.text
            
            // 先卸载掉原创微博底部约束
            self.originalViewBottomConstraint?.uninstall()
            
            // 有配图
            if status?.pic_urls?.count != 0{
                
                // 00- 给我们的配图赋值
                pictureView.pictureInfo = status?.pic_urls
                
                // 01-我们原创微博的底部 == 配图底部 + 10
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                })
                
                
                // 02-显示配图(考虑到cell复用问题)
                pictureView.hidden = false
                
            }else{
                // 没有配图
                // 01-我们原创微博的底部 == 微博正文底部 + 10
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                })
                // 02-隐藏我们的配图(考虑到cell复用问题)
                pictureView.hidden = true
            }
            
        }
    }
    
    // 通过服务器返回来的int 数据 创建UIImage
    ///// 会员等级 0-6
    func getmbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7{
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else{
            return UIImage(named:"common_icon_membership_expired")
        }
    }
    
    // 认证图片逻辑处理
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    func getverifiedImage(verified: Int) -> UIImage?{
        switch verified{
        case 0:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        // 01 - 添加控件
        // 02 - 添加约束
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 01
        headImageView.snp_makeConstraints { (make) -> Void in
            //            make.leading.equalTo(10)
            //            make.top.equalTo(10)
            // 偷懒方法
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        
        // 03
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        // 04
        timeLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        // 05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLable.snp_trailing).offset(10)
            make.bottom.equalTo(timeLable)
        }
        
        // 06
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        // 07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(headImageView)
            make.width.equalTo(ScreenW - 20)
        }
        // 08
        pictureView.snp_makeConstraints { (make) -> Void in
            
            // 该逻辑已经在pictureView做完了
            // make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        // 给我们原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
            
        }
        
    }
    
    // MARK: - 懒加载控件
    // 用户头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 用户昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 14)
    // 会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 时间
    private lazy var timeLable: UILabel = UILabel(textColor: themColor, textFont: 10)
    // 来自
    private lazy var sourceLabel: UILabel = UILabel(textColor: themColor, textFont: 10)
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vgirl"))
    // 微博正文
    private lazy var contentLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.blackColor(), textFont: 14)
        // lab.text = "微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文"
        lab.numberOfLines = 0
        return lab
    }()
    // 微博配图
    private lazy var pictureView: YZMPictureView = YZMPictureView()
    
    
    
    
}
